//
//  KzMutableDic.swift
//  swifttest1
//
//  Created by zcj on 2024/10/15.
//

import Foundation
import Combine

class KzMutableDic<K:Hashable,V>: ObservableObject {
    @Published var dic: Dictionary<K, V>
    @Published var state: Bool = false
    init(dic: Dictionary<K, V>, state: Bool) {
        self.dic = dic
        self.state = state
    }
    
    func add(key:K, value:V){
        dic[key] = value
    }
    func removeAll(){
        dic.removeAll()
    }
    
    func removeValue(key:K){
        dic.removeValue(forKey: key)
    }
    
    func updateData(){
        objectWillChange.send()
    }
    
    subscript(key:K)->V?{
        get{
            return dic[key] ?? nil
        }
        set{
            dic[key] = newValue
        }
    }
    
    func keys() -> Dictionary<K, V>.Keys{
        return dic.keys
    }
    
    func forEach(_ body: @escaping((key: K, value: V) )->Void){
        dic.forEach { (key: K, value: V) in
            body((key: key, value: value))
        }
    }
    
    func count() -> Int{
        return dic.count
    }
}
